
package control.servlet;

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import model.info.EquipmentList;
import bean.CharacterBEAN;
import bean.EquipmentBEAN;
import dao.CharacterDAO;
import dao.EquipmentDAO;

/**
 * Servlet implementation class Shop
 */
public class Shop extends HttpServlet {
	private static final long	serialVersionUID	= 1L;
	private CharacterDAO		characterDAO;
	private EquipmentDAO		equipmentDAO;

	public void init( ServletConfig config ) throws ServletException {
		characterDAO = (CharacterDAO) config.getServletContext().getAttribute(
				"CharacterDAO" );
		equipmentDAO = (EquipmentDAO) config.getServletContext().getAttribute(
				"EquipmentDAO" );
	}

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Shop() {
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet( HttpServletRequest request,
			HttpServletResponse response ) throws ServletException, IOException {
		String action = request.getParameter( "action" );
		HttpSession session = request.getSession();
		if( session.getAttribute( "ID" ) != null ) {
			int accountID = (Integer) session.getAttribute( "ID" );
			CharacterBEAN characterBEAN = characterDAO
					.getCharacterByID( accountID );
			EquipmentBEAN equipmentBEAN = equipmentDAO
					.getEquipmentByID( accountID );
			request.setAttribute( "character", characterBEAN );
			request.setAttribute( "equipmentBEAN", equipmentBEAN );
			if( action.equals( "weapons" ) ) {
				goTo( "WEB-INF/logged/shop/weapons.jsp", request, response );
				return;
			} else if( action.equals( "protection" ) ) {
				goTo( "WEB-INF/logged/shop/protection.jsp", request, response );
				return;
			} else if( action.equals( "bullets" ) ) {
				goTo( "WEB-INF/logged/shop/bullets.jsp", request, response );
				return;
			} else {
				goTo( "WEB-INF/logged/shop/shopOverview.jsp", request, response );
			}
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost( HttpServletRequest request,
			HttpServletResponse response ) throws ServletException, IOException {
		String action = request.getParameter( "action" );
		HttpSession session = request.getSession();
		if( session.getAttribute( "ID" ) != null ) {
			int accountID = (Integer) session.getAttribute( "ID" );
			CharacterBEAN characterBEAN = characterDAO
					.getCharacterByID( accountID );
			EquipmentBEAN equipmentBEAN = equipmentDAO
					.getEquipmentByID( accountID );
			if( action.startsWith( "buyWeapon" ) ) {
				int weaponId = Integer.parseInt( action.substring( 9 ) );
				if( characterBEAN.getMoneyCash() >= getItemCost( weaponId ) ) {
					characterDAO.setCashMoney( characterBEAN.getMoneyCash()
							- getItemCost( weaponId ), characterBEAN
							.getCharacterID() );
					equipmentBEAN = buyItem( weaponId, equipmentBEAN, 0 );
				} else {
					request.setAttribute( "message",
							"You do not have enough money to buy that!" );
				}
				request.setAttribute( "character", characterBEAN );
				request.setAttribute( "equipmentBEAN", equipmentBEAN );
				goTo( "WEB-INF/logged/shop/weapons.jsp", request, response );
				return;
			} else if( action.startsWith( "buyArmor" ) ) {
				int armorId = Integer.parseInt( action.substring( 8  ) );
				if( characterBEAN.getMoneyCash() >= getItemCost( armorId + 9 ) ) {
					characterDAO.setCashMoney( characterBEAN.getMoneyCash()
							- getItemCost( armorId + 9 ), characterBEAN
							.getCharacterID() );
					equipmentBEAN = buyItem( armorId + 9, equipmentBEAN, 0 );
				} else {
					request.setAttribute( "message",
							"You do not have enough money to buy that!" );
				}
				request.setAttribute( "character", characterBEAN );
				request.setAttribute( "equipmentBEAN", equipmentBEAN );
				goTo( "WEB-INF/logged/shop/protection.jsp", request, response );
				return;
			} else if( action.startsWith( "buyBullet" ) ) {
				int bullets = Integer
						.parseInt( request.getParameter( "amount" ) );
				if( characterBEAN.getMoneyCash() >= ( getItemCost( 30 ) * bullets ) ) {
					characterDAO.setCashMoney( characterBEAN.getMoneyCash()
							- ( getItemCost( 30 ) * bullets ), characterBEAN
							.getCharacterID() );
					equipmentBEAN = buyItem( 30, equipmentBEAN, bullets );
				} else {
					request.setAttribute( "message",
							"You do not have enough money to buy that!" );
				}
				request.setAttribute( "character", characterBEAN );
				request.setAttribute( "equipmentBEAN", equipmentBEAN );
				goTo( "WEB-INF/logged/shop/bullets.jsp", request, response );
				return;
			} else if( action.startsWith( "buyGuard" ) ) {
				System.out.println( "buyGuard" );
				int guardId = Integer.parseInt( action.substring( 8 ) );
				System.out.println( "guardId: " + guardId );
				if( characterBEAN.getMoneyCash() >= getItemCost( guardId + 19 ) ) {
					characterDAO.setCashMoney( characterBEAN.getMoneyCash()
							- getItemCost( guardId + 19 ), characterBEAN
							.getCharacterID() );
					equipmentBEAN = buyItem( guardId + 19, equipmentBEAN, 0 );
				} else {
					request.setAttribute( "message",
							"You do not have enough money to buy that!" );
				}
				request.setAttribute( "character", characterBEAN );
				request.setAttribute( "equipmentBEAN", equipmentBEAN );
				goTo( "WEB-INF/logged/shop/protection.jsp", request, response );
				return;
			} else {
				goTo( "WEB-INF/logged/shop/shopOverview.jsp", request, response );
				return;
			}
		}
	}

	private void goTo( String action, HttpServletRequest request,
			HttpServletResponse response ) throws ServletException, IOException {
		try {
			RequestDispatcher dispatcher = request
					.getRequestDispatcher( action );
			dispatcher.forward( request, response );
		} catch( Exception e ) {
			e.printStackTrace();
		}
	}

	private int getItemCost( int itemId ) {
		for( EquipmentList eql : EquipmentList.values() ) {
			if( itemId == eql.getId() ) { return eql.getPrice(); }
		}
		return 0;
	}

	private EquipmentBEAN buyItem( int weaponId, EquipmentBEAN equipmentBEAN,
			int bullets ) {
		switch( weaponId ) {
		case 1: // Sniper
			equipmentBEAN.setSniperRifle( true );
			equipmentDAO.updateEquipment( equipmentBEAN );
			break;
		case 2: // UziSMG
			equipmentBEAN.setUziSMG( true );
			equipmentDAO.updateEquipment( equipmentBEAN );
			break; 
		case 3: // Desert Eagle
			equipmentBEAN.setDesertEagle( true );
			equipmentDAO.updateEquipment( equipmentBEAN );
			break;
		case 4: // Barak SP21
			equipmentBEAN.setBarakSP21( true );
			equipmentDAO.updateEquipment( equipmentBEAN );
			break;
		case 5: // Colt M1911
			equipmentBEAN.setColtM1911( true );
			equipmentDAO.updateEquipment( equipmentBEAN );
			break;
		case 6: // Knife
			equipmentBEAN.setKnife( true );
			equipmentDAO.updateEquipment( equipmentBEAN );
			break;
		case 10: // Bullet free armor
			equipmentBEAN.setBulletFreeArmor( true );
			equipmentDAO.updateEquipment( equipmentBEAN );
			break;
		case 11: // Bullet free helmet
			equipmentBEAN.setBulletFreeHelmet( true );
			equipmentDAO.updateEquipment( equipmentBEAN );
			break;
		case 12: // Bomb armor
			equipmentBEAN.setBombArmor( true );
			equipmentDAO.updateEquipment( equipmentBEAN );
			break;
		case 20: // Bodyguards
			equipmentBEAN.setBodyguard( 1 );
			equipmentDAO.updateEquipment( equipmentBEAN );
			break;
		case 21:
			equipmentBEAN.setBodyguard( 2 );
			equipmentDAO.updateEquipment( equipmentBEAN );
			break;
		case 22:
			equipmentBEAN.setBodyguard( 3 );
			equipmentDAO.updateEquipment( equipmentBEAN );
			break;
		case 23:
			equipmentBEAN.setBodyguard( 4 );
			equipmentDAO.updateEquipment( equipmentBEAN );
			break;
		case 24:
			equipmentBEAN.setBodyguard( 5 );
			equipmentDAO.updateEquipment( equipmentBEAN );
			break;
		case 30:
			equipmentBEAN.setBullet( equipmentBEAN.getBullet() + bullets );
			equipmentDAO.updateEquipment( equipmentBEAN );
			break;
		}
		return equipmentBEAN;
	}
}
